package com.zjc.stock.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class DoubleUtil {

    public static double add(double num1, double num2) {
        BigDecimal n1 = new BigDecimal(num1 + "");
        BigDecimal n2 = new BigDecimal(num2 + "");
        return n1.add(n2).setScale(2, RoundingMode.UP).doubleValue();
    }

    public static double subtract(double num1, double num2) {
        BigDecimal n1 = new BigDecimal(num1 + "");
        BigDecimal n2 = new BigDecimal(num2 + "");
        BigDecimal r = n1.subtract(n2);
        r.setScale(2, RoundingMode.UP);
        return r.doubleValue();
    }

    public static double multiply(double num1, double num2) {
        BigDecimal n1 = new BigDecimal(num1 + "");
        BigDecimal n2 = new BigDecimal(num2 + "");
        return n1.multiply(n2).setScale(2, RoundingMode.UP).doubleValue();
    }

    public static double divide(double num1, double num2) {
        BigDecimal n1 = new BigDecimal(num1 + "");
        BigDecimal n2 = new BigDecimal(num2 + "");
        BigDecimal r = n1.divide(n2, 2, RoundingMode.UP);

        return r.doubleValue();
    }
}
